package com.ravi.core.queue;

import com.ravi.dao.CheckRoomDAO;
import com.ravi.entity.CheckRoom;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class HashQueueManager {
	public static final ConcurrentHashMap<Integer, CheckUpQueue> queueManager = new ConcurrentHashMap<>();

	@Autowired
	private CheckRoomDAO checkRoomDAO;
	/**
	 * 添加队列
	 * @param queue
	 */
	public static void addQueue(CheckUpQueue queue) {
		queueManager.put(queue.getCheckRoom().getRoomId(), queue);
	}

	/**
	 * 获取队列
	 * @param queueId
	 * @return
	 */
	public static CheckUpQueue getQueue(Integer queueId) {
		return queueManager.get(queueId);
	}

	@PostConstruct
	public void init() {
		List<CheckRoom> checkRooms = checkRoomDAO.getAllCheckRooms();
		CheckUpQueue checkQueue = null;
		for (CheckRoom room : checkRooms) {
			checkQueue = new CheckUpQueue(room);
			addQueue(checkQueue);
		}
	}

	@PreDestroy
	public void destroy() {
		//系统运行结束
	}


}
